import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:provider/provider.dart';

import 'package:qaun_ni_dev/pages/brushsHappy/brush_detail.dart';
import 'package:qaun_ni_dev/provider/brush_provider.dart';



class BrushHappyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('刷刷乐管理'),
      ),
      body: BrushPageList()
    );
  }
}

class BrushPageList extends StatefulWidget {
  @override
  _BrushPageListState createState() => _BrushPageListState();

}

class _BrushPageListState extends State<BrushPageList> {

  GlobalKey<RefreshFooterState> _footerKey = new GlobalKey<RefreshFooterState>();
  GlobalKey<RefreshHeaderState> _headerKey = new GlobalKey<RefreshHeaderState>();

  void initState() {
    super.initState();
    _getList();
  }

  void _getList() {
    Provider.of<BrushProvider>(context,listen: false).getBrushList(context);
  }


  @override
  Widget build(BuildContext context) {
    return Container(
      child: Consumer<BrushProvider>(
          builder: (context,data,_){
            if(data.brushDataList.length >0 ) {
              return Container(
                padding: EdgeInsets.only(top: 10,left: 15,right: 15),
                child: EasyRefresh(
                    refreshFooter: ClassicsFooter(
                        key:_footerKey,
                        bgColor:Colors.white,
                        textColor:Colors.pink,
                        moreInfoColor: Colors.pink,
                        showMore:true,
                        noMoreText: '没有更多了',
                        moreInfo:'加载中',
                        loadReadyText:'上拉加载'
                    ),
                    refreshHeader:ClassicsHeader(
                        key: _headerKey,
                        refreshText: "下拉刷新数据",
                        refreshReadyText: "准备刷新数据",
                        refreshingText: "数据刷新中...",
                        refreshedText: "刷新完成",
                        bgColor: Colors.blue,
                        textColor: Colors.white,
                        moreInfoColor: Colors.white,
                        refreshHeight: 70.0,
                        isFloat: false,
                        showMore: false,
                        moreInfo: "Updated at %T"
                    ),
                    child: ListView.builder(
                        itemCount: data.brushDataList.length,
                        itemBuilder: (context,index) {
                          return _listWidget(data.brushDataList,index);
                        }
                    ),
                    loadMore: () async {
                      print('加载更多的');
                      Provider.of<BrushProvider>(context,listen: false).getBrushMoreList(context);
                    },
                    onRefresh: () async{
                      print('上拉刷新');
                      Provider.of<BrushProvider>(context,listen: false).getBrushList(context);
                    }
                ),
              );
            } else {
              return Text('暂无数据哦，请稍等');
            }
          }
      ),
    );
  }
  Widget _listWidget (List brushList,int index) {
    return InkWell(
      onTap: () {
        Navigator.push(context, MaterialPageRoute(
            builder: (context) {
              return BrushDetail(brushList[index]);
            }
        ));
      },
      child: Container(
        margin: EdgeInsets.only(bottom: 10),
        decoration: BoxDecoration(
            border: Border.all(width: 1,color: Colors.black12),
            borderRadius: BorderRadius.all(Radius.circular(6))
        ),
        child: Row(
          children: <Widget>[
            _leftOrderNumber(brushList,index),
            _rightContent(brushList,index),
          ],
        ),
      ),
    );
  }

  Widget _leftOrderNumber(brushList,index) {
    return Container(
      margin: EdgeInsets.only(right: 10),
      padding: EdgeInsets.only(left: 6),
      width: 50,
      child: Column(
        children: <Widget>[
          Text('序号${index}',
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }

  Widget _rightContent(brushList,index) {
    int postType = brushList[index].postType;
    String postTypeText = '';
    switch (postType) {
      case 0: { postTypeText = '日常动态'; }
      break;
      case 2: { postTypeText = '注册审核视频'; }
      break;
      case 3: { postTypeText = '认证视频'; }
      break;
    }



    return Container(
      padding: EdgeInsets.only(left: 8,right: 6,top: 6,bottom: 6),
      decoration: BoxDecoration(
          border: Border(
            left: BorderSide(color: Colors.grey,width: 1),
          )
      ),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '用户昵称:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${brushList[index].userInfo.nickName}'),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '动态类型:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${postTypeText}'),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '动态内容:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${brushList[index].postContent}'),
              ),
            ],
          ),

        ],
      ),
    );
  }


}